package co.com.desarrollo.pedidos.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import co.com.desarrollo.pedidos.dao.ConfisysDAO;
import co.com.desarrollo.pedidos.entities.Confisys;

@Repository("confisysDAO")
public class JpaConfisysDAOImpl implements ConfisysDAO {

	@PersistenceContext
	private EntityManager entityManager;
	
	@Transactional
	public void delete(long confisysId) {
		entityManager.remove(entityManager.find(Confisys.class, confisysId));
	}

	@Transactional(readOnly = true)
	public List<Confisys> findAll() {
		return entityManager.createQuery("from Confisys",Confisys.class).getResultList();
	}

	@Transactional(readOnly = true)
	public Confisys findById(long confisysId) {		
		return entityManager.find(Confisys.class, confisysId);
	}

	@Transactional
	public Confisys store(Confisys confisys) {
		return entityManager.merge(confisys);
	}

	@Transactional(readOnly = true)
	public Confisys findByName(String nombre) {
		return entityManager.createQuery("from Confisys c where c.nombre = '"+nombre+"'", Confisys.class).getSingleResult();
	}

}
